package org.gordonframework.site.forms.elements {
	import org.gordonframework.site.events.GFormElementEvent;	
	import org.gordonframework.site.forms.elements.skins.GCheckBoxSkin;	
	import org.gordonframework.site.forms.elements.GElement;
	
	import flash.events.MouseEvent;	

	/**
	 * @author eriarv
	 */
	public class GCheckBox extends GElement {
		
		public static const TYPE : String = "CheckBox";
		public static const ON : String = "on";		public static const OFF : String = "off";
		
		
		public function GCheckBox( id : String, value : String = "", mandatory : Boolean = false, text : String = "", skinClass : Class = null) {
			super(TYPE, id, value, mandatory, text, skinClass);
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function clickHandler(event : MouseEvent) : void {
			isOn = !isOn;
		}

		public override function get isValid() : Boolean {
			if ( mandatory ) {
				return (value == ON);
			} else {
				return true;
			}
		}
		
		public function set isOn( value : Boolean ) : void {
			this.value = value ? ON : OFF;
			checkBoxSkin.isOn = value;
			dispatchEvent(new GFormElementEvent(GFormElementEvent.CHANGE));
		}
		
		public function get isOn() : Boolean {
			return (value == ON);
		}
		
		private function get checkBoxSkin() : GCheckBoxSkin {
			return skin as GCheckBoxSkin;
		}
	}
}
